package com.shuiyihan.design.structure.proxy.cglib;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 代理人
 *
 * @since 2021/8/27
 **/
public class CglibMedium implements MethodInterceptor {


    //被代理对象
    private Object instance;

    public CglibMedium(Object instance) {
        this.instance = instance;
    }


    /**
     *
     * @param o 代理类
     * @param method 代理方法
     * @param objects 参数
     * @param methodProxy 被代理的原始方法
     * @return
     * @throws Throwable 异常
     */
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        objects[0] = "我又双双带 " + objects[0];

        return method.invoke(instance,objects);
    }
}
